{template 'header', 'mobile'}
<div id="head-bar">
<div class="head-bar">
<div class="head-bar-back">
<a href="{$back_link}" data-direction="reverse"><img src="static/img/icon-back.png" width="24" height="24"/><span>返回</span></a>
</div>
<div class="head-bar-title">{$head_name}</div>
<div class="head-bar-right" onclick="Dsheet('<a href=&#34;{$back_link}&#34;><span>确定取消</span></a>', '我再想想', '确定要取消此订单吗？');"><span>取消</span></div>
</div>
<div class="head-bar-fix"></div>
</div>
{if $need_addr}
<div class="main bd-b" style="padding:6px 16px;" onclick="Dsheet('<a href=&#34;{$addr_url}&#34; data-transition=&#34;slideup&#34;><span>更换地址</span></a>', '不用更换', '确定要更换收货地址吗？');">
	<div style="line-height:30px;font-size:16px;"><span class="f_r">{$addr[mobile]}</span>收货人：{$addr[truename]}</div>
	<div style="line-height:30px;">{$addr[address]}</div>
</div>
{/if}
<div class="blank-20"></div>
<style type="text/css">
.list-buy {padding:0 0 0 16px;}
.list-buy div {height:44px;overflow:hidden;font-size:16px;line-height:44px;padding-right:16px;}
.list-buy div span {float:right;color:#999999;font-size:14px;}
.list-buy div em {display:block;width:44px;height:44px;line-height:44px;font-style:normal;color:#999999;background:#F3F3F3;float:left;text-align:center;}
.list-buy div strong {font-weight:normal;color:#FF0000;}
.list-buy input {width:100%;height:44px;line-height:24px;border:none;padding:0;font-size:16px;}
</style>
<div class="list-img bd-t">
<a href="index.php?moduleid={$moduleid}&itemid={$t[itemid]}"><img src="{if $t[thumb]}{$t[thumb]}{else}static/img/nopic-60.png{/if}" width="60" height="60" alt="" onerror="this.src='static/img/nopic-60.png';"/></a>
<ul>
<li><a href="index.php?moduleid={$moduleid}&itemid={$t[itemid]}"><strong>{$t[title]}</strong></a></li>
<li class="price">{$DT[money_sign]}<span id="price" class="price">{$t[price]}</span>{if isset($t[unit]) && $t[unit]}/{$t[unit]}{/if}</li>
<li{if $t[vip]} class="vip" title="{VIP}:{$t[vip]}"{/if}><a href="index.php?moduleid=4&username={$t[username]}"><span>{$t[company]}</span></a></li>
</ul>
</div>
<div class="main">
	<form id="purchase-form">
	<input type="hidden" name="ok" value="1"/>
	<input type="hidden" name="moduleid" value="{$moduleid}"/>
	<input type="hidden" name="itemid" value="{$itemid}"/>
	{if $module == 'mall'}
	<input type="hidden" name="s1" value="{$s1}"/>
	<input type="hidden" name="s2" value="{$s3}"/>
	<input type="hidden" name="s3" value="{$s3}"/>
	<input type="hidden" id="fee_start_1" value="{$t[fee_start_1]}"/>
	<input type="hidden" id="fee_step_1" value="{$t[fee_step_1]}"/>
	<input type="hidden" id="fee_start_2" value="{$t[fee_start_2]}"/>
	<input type="hidden" id="fee_step_2" value="{$t[fee_step_2]}"/>
	<input type="hidden" id="fee_start_3" value="{$t[fee_start_3]}"/>
	<input type="hidden" id="fee_step_3" value="{$t[fee_step_3]}"/>
	{/if}
	<div class="list-buy">
		<div class="bd-b" style="padding-right:0;"><span><em style="font-size:20px;" onclick="alter('-');">―</em><input type="tel" name="number" id="number" value="{$t[minamount]}" style="width:64px;float:left;text-align:center;color:#555555;"/><em style="font-size:24px;" onclick="alter('+');">+</em></span>购买数量</div>
		{if $module == 'sell'}
		<div class="bd-b"><span>联系卖家</span>配送方式</div>
		{elseif $module == 'mall'}
			<div class="bd-b"><span>
			<strong id="fee">{$DT[money_sign]}0.00</strong>&nbsp;
			<select name="express" id="express" onchange="calculate();">
			{if $t[express_name_1] == '包邮'}
				{if $t[fee_start_1]>0}
				{if $t[fee_start_2]>0}<option value="2" data-2>{$t[express_name_2]}</option>{/if}
				{if $t[fee_start_3]>0}<option value="3" data-3>{$t[express_name_3]}</option>{/if}
				<option value="-1" data--1>满{$t[fee_start_1]}包邮</option>
				{if $t[fee_start_2]>0 || $t[fee_start_3]>0}
				{else}
				<option value="0" data-0>联系卖家</option>
				{/if}
				{else}
				<option value="0" data-0>包邮</option>
				{/if}
			{elseif $t[fee_start_1]>0 || $t[fee_start_2]>0 || $t[fee_start_3]>0}
				{if $t[fee_start_1]>0}<option value="1">{$t[express_name_1]}</option>{/if}
				{if $t[fee_start_2]>0}<option value="2">{$t[express_name_2]}</option>{/if}
				{if $t[fee_start_3]>0}<option value="3">{$t[express_name_3]}</option>{/if}
			{else}
			<option value="0">联系卖家</option>
			{/if}
			</select>
			</span>配送方式</div>
			{if $t[m1] || $t[m2] || $t[m3]}
			<div class="bd-b"><span>{if $t[m1]}{$t[n1]}:{$t[m1]}&nbsp;{/if}{if $t[m2]}{$t[n2]}:{$t[m2]}&nbsp;{/if}{if $t[m3]}{$t[n3]}:{$t[m3]}&nbsp;{/if}</span>商品参数</div>
			{/if}
			{if $t[cod]}
			<div class="bd-b"><span>{if $t[cod]==2}<select name="cod"><option value="1">货到付款</option><option value="0">在线支付</option></select>{else}货到付款{/if}</span>付款方式</div>
			{/if}
		{/if}
		{if !$need_addr}
		<div class="bd-b"><span><input type="tel" name="mobile" id="mobile" placeholder="手机号码:" value="{$addr[mobile]}" style="width:136px;"/></span>手机号码</div>
		{/if}
		<div class="bd-b"><input type="text" name="note" id="note" placeholder="备注:"/></div>
	</div>
	<div class="bd-b" style="line-height:44px;text-align:right;padding-right:16px;font-size:16px;">总价:<span class="f_red" style="font-size:20px;">{$DT[money_sign]}<span id="total">{$t[price]}</span></span></div>
	</form>
</div>
<div class="main" style="padding:10px;" onclick="Dpurchase();">
	<div class="btn-green">确认购买</div>
</div>
<script type="text/javascript">
var price = {$t[price]};
var mina = {$t[minamount]};
var maxa = {$t[amount]};
{if $module == 'mall'}
function get_price() {
	var p1 = {$t[p1]};
	var p2 = {$t[p2]};
	var p3 = {$t[p3]};
	var a1 = {$t[a1]};
	var a2 = {$t[a2]};
	var a3 = {$t[a3]};
	var number = parseInt($('#number').val());
	if(a2 > 0) {
		if(a3 > 1 && number > a3) return p3;
		if(a2 > 1 && number > a2) return p2;
	}
	return p1;
}
function get_fee() {
	var es = $('#express').html();
	var number = parseInt($('#number').val());
	if(es.indexOf('data--1') != -1) {
		if(parseFloat(get_price()*number) >= parseFloat($('#fee_start_1').val())) {
			$('#express').val(-1);
		} else {
			if(es.indexOf('data-0') != -1) {
				$('#express').val(0);
			} else if(es.indexOf('data-2') != -1) {
				$('#express').val(2);
			} else if(es.indexOf('data-3') != -1) {
				$('#express').val(3);
			}
		}
	}
	var k = $('#express').val();
	return k > 0 ? parseFloat($('#fee_start_'+k).val()) + parseFloat($('#fee_step_'+k).val())*(number-1) : 0.00;
}
{/if}
function alter(t) {
	var number = parseInt($('#number').val());
	if(t == '+') {
		$('#number').val(number+1);
	} else {
		$('#number').val(number-1);
	}
	calculate();
}
function calculate() {
	var number = parseInt($('#number').val());
	if(mina && number < mina) number = mina;
	if(maxa && number > maxa) number = maxa;
	if(number < 1) number = 1;
	$('#number').val(number);
{if $module == 'mall'}
	price = get_price();
	$('#price').html(price);
{/if}
	var money = price*number;
{if $module == 'mall'}
	fee = get_fee();
	$('#fee').html('{$DT[money_sign]}'+fee.toFixed(2));
	money += fee;
{/if}
	$('#total').html(money.toFixed(2));
}
function Dpurchase() {
	{if !$need_addr}
	if(!$('#mobile').val().match(/^1[3|4|5|7|8]{1}[0-9]{9}$/)) {
		Dtoast('请填写正确的手机号码');
		return false;
	}
	{/if}
	$.post('purchase.php', $('#purchase-form').serialize(), function(data) {
		if(data.indexOf('ok|') != -1) {
			Dtoast('订单创建成功，请尽快支付');
			id = data.substr(3);
			setTimeout(function() {
				Dsheet('<a href="{$MURL}{$order_name}.php?action=update&step=detail&itemid='+id+'" rel="external"><span>查看订单</span></a>|<a href="{$MURL}{$order_name}.php?action=update&step=pay&itemid='+id+'" rel="external"><span>支付订单</span></a>|<a href="index.php?moduleid={$moduleid}"><span>继续购物</span></a>');
			}, 1000);
		} else if(data == 'mobile') {
			Dtoast('请填写正确的手机号码');
		} else {
			Dtoast('订单创建失败，请重试');
			setTimeout(function() {
				window.location.reload();
			}, 1000);

		}
	});
}
$(document).on('pageinit', function(event) {
	$('input').on('blur', function(){window.scrollTo(0,0);});
	$('#number').on('blur', function(){calculate();});
	calculate();
});
</script>
{template 'footer', 'mobile'}
